package com.tjs.lucky.domain.rule.service.engine.impl;

import com.tjs.lucky.domain.rule.mdel.aggregates.TreeRuleRich;
import com.tjs.lucky.domain.rule.mdel.req.DecisionValueReq;
import com.tjs.lucky.domain.rule.mdel.res.EngineResult;
import com.tjs.lucky.domain.rule.mdel.vo.TreeNodeVO;
import com.tjs.lucky.domain.rule.repository.IRuleRepository;
import com.tjs.lucky.domain.rule.service.engine.EngineBase;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: 田佳澍
 * @Description:
 * @DateTime: 2023/5/29 17:29
 **/
@Service("ruleEngineHandle")
public class RuleEngineHandle extends EngineBase {
    @Resource
    private IRuleRepository ruleRepository;

    @Override
    public EngineResult process(DecisionValueReq valueReq) {
        // 决策规则树
        TreeRuleRich treeRuleRich = ruleRepository.queryTreeRuleRich(valueReq.getTreeId());
        if (null == treeRuleRich) {
            throw new RuntimeException("Tree Rule is null!");
        }

        // 决策节点
        TreeNodeVO treeNodeInfo = engineDecisionMaker(treeRuleRich, valueReq);

        // 决策结果
        return new EngineResult(valueReq.getUserId(), treeNodeInfo.getTreeId(), treeNodeInfo.getTreeNodeId(), treeNodeInfo.getNodeValue());
    }

}
